﻿using UnityEngine;
using System.Collections;

public class Pickup : MonoBehaviour {

	string type = "";
	float healthAmount = 0;
	Inventory.InventoryItem itemToAdd;
	Inventory inventory;
	public Sprite healthTexture;
	public Sprite pistol;
	public Sprite laser;
	public Sprite shotGun;
	public Sprite machineGun;
	public Sprite key;
	public Sprite armour;
	void Start() {
		inventory = GameObject.FindGameObjectWithTag ("StateManager").GetComponent<Inventory> ();
		transform.position += new Vector3 (0, 0.1f, 0);
	}
	public void SetupPickup(string givenType, float amount = 0, Inventory.InventoryItem item = new Inventory.InventoryItem()){
		type = givenType;
		if (type == "Health") {
			GetComponent<SpriteRenderer>().sprite = healthTexture;
			transform.localScale = new Vector3(0.1f,0.1f,0.1f);
			healthAmount = amount;
		}
		if (type == "Item") {
			itemToAdd = item;
			if(itemToAdd.type == "Armour") 
				GetComponent<SpriteRenderer>().sprite = armour;
			if(itemToAdd.type == "Weapon") {
				switch(itemToAdd.fireMode) {
				case Weapon.WeaponType.Semi:
					GetComponent<SpriteRenderer>().sprite = pistol;
					break;
				case Weapon.WeaponType.Auto:
					GetComponent<SpriteRenderer>().sprite = machineGun;
					break;
				case Weapon.WeaponType.Laser:
					GetComponent<SpriteRenderer>().sprite = laser;
					break;
				case Weapon.WeaponType.Shotgun:
					GetComponent<SpriteRenderer>().sprite = shotGun;
					break;
				}
			}

		}
		if( type == "Key") {
			GetComponent<SpriteRenderer>().sprite = key;
			itemToAdd = new Inventory.InventoryItem("Key", Weapon.WeaponType.Semi, 0,0,0,0,0);
		}
	}

	void OnTriggerEnter(Collider other) {
		if (other.tag == "Player") {
			if (type == "Health") {
				other.GetComponent<Player>().health += healthAmount;
			}
			if (type == "Item" || type == "Key") {
				inventory.AddInventoryItem(itemToAdd);
			}
			Destroy(gameObject);
		}
	}
}
